home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Dave Mark / Mac C Primer 1 (SC++7) / 3.2 - Mondrian / Mondrian.c next >
Encoding:
C/C++ Source or Header  |  1994-04-27  |  2.5 KB  |  141 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  Mondrian Code from Chapter Three of                    */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11. #define    kBaseResID            128
  12. #define    kMoveToFront        (WindowPtr)-1
  13. #define kRandomUpperLimit    32768
  14.  
  15.  
  16. /*************/
  17. /*  Globals  */
  18. /*************/
  19.  
  20. long    gFillColor = blackColor;
  21.  
  22.  
  23. /***************/
  24. /*  Functions  */
  25. /***************/
  26.  
  27. void    ToolBoxInit( void );
  28. void    WindowInit( void );
  29. void    MainLoop( void );
  30. void    DrawRandomRect( void );
  31. void    RandomRect( Rect *rectPtr );
  32. short    Randomize( short range );
  33.  
  34.  
  35. /****************** main ***************************/
  36.  
  37. void    main( void )
  38. {
  39.     ToolBoxInit();
  40.     WindowInit();
  41.     MainLoop();
  42. }
  43.  
  44.  
  45. /****************** ToolBoxInit *********************/
  46.  
  47. void    ToolBoxInit( void )
  48. {
  49.     InitGraf( &qd.thePort );
  50.     InitFonts();
  51.     InitWindows();
  52.     InitMenus();
  53.     TEInit();
  54.     InitDialogs( 0L );
  55.     InitCursor();
  56. }
  57.  
  58.  
  59. /****************** WindowInit ***********************/
  60.  
  61. void    WindowInit( void )
  62. {
  63.     WindowPtr    window;
  64.  
  65.     window = GetNewWindow( kBaseResID , nil,
  66.                                     kMoveToFront );
  67.     
  68.     if ( window == nil
  69.      )
  70.     {
  71.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  72.         ExitToShell();
  73.     }
  74.     
  75.     ShowWindow( window );
  76.     SetPort( window );
  77. }
  78.  
  79.  
  80. /****************** MainLoop ***********************/
  81.  
  82. void    MainLoop( void )
  83. {
  84.     GetDateTime( (unsigned long *)(&qd.randSeed) );
  85.     
  86.     while ( ! Button() )
  87.     {
  88.         DrawRandomRect();
  89.         
  90.         if ( gFillColor == blackColor )
  91.             gFillColor = whiteColor;
  92.         else
  93.             gFillColor = blackColor;
  94.     }
  95. }
  96.  
  97.  
  98. /****************** DrawRandomRect *****************/
  99.  
  100. void    DrawRandomRect( void )
  101. {
  102.     Rect    randomRect;
  103.     
  104.     RandomRect( &randomRect );
  105.     ForeColor( gFillColor );
  106.     PaintOval( &randomRect );
  107. }
  108.  
  109.  
  110. /****************** RandomRect *********************/
  111.  
  112. void    RandomRect( Rect *rectPtr )
  113. {
  114.     WindowPtr    window;
  115.  
  116.     window = FrontWindow();
  117.     
  118.     rectPtr->left = Randomize( window->portRect.right
  119.         - window->portRect.left );
  120.     rectPtr->right = Randomize( window->portRect.right
  121.         - window->portRect.left );
  122.     rectPtr->top = Randomize( window->portRect.bottom
  123.         - window->portRect.top );
  124.     rectPtr->bottom = Randomize( window->portRect.bottom
  125.         - window->portRect.top );
  126. }
  127.  
  128.  
  129. /****************** Randomize **********************/
  130.  
  131. short    Randomize( short range )
  132. {
  133.     long    randomNumber;
  134.     
  135.     randomNumber = Random();
  136.     
  137.     if ( randomNumber < 0 )
  138.         randomNumber *= -1;
  139.     
  140.     return( (randomNumber * range) / kRandomUpperLimit );
  141. }